﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace ECommerce.Administration.Controls
{
	public partial class OrderList : AdminBaseUsercontrol
	{
		private Service.IOrderService _OrdeService;
		private Service.IPaymentMethodService _PaymentMethodService;
		private Service.IShippingMethodService _ShippingMethodService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (SiteContext.Current == null)
			{
				Server.Transfer("~Administration/AdminLogin.aspx");
			}

			if (!IsPostBack)
			{
				ButtonSearch.Text = GetLocalResourceString("Admin.SearchButton.Text");

				BindOrderStatus();

				BindPaymentStatus();

				BindShippingStatus();
			}
		}

		protected void GridOrders_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[0].Text = GetLocalResourceString("Admin.OrderList.OrderTotal.Header");
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.OrderList.OrderStatus.Header");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.OrderList.PaymentStatus.Header");
				e.Row.Cells[3].Text = GetLocalResourceString("Admin.OrderList.ShippingStatus.Header");
				e.Row.Cells[4].Text = GetLocalResourceString("Admin.OrderList.CustomerName.Header");
				e.Row.Cells[5].Text = GetLocalResourceString("Admin.OrderList.View.Header");
				e.Row.Cells[6].Text = GetLocalResourceString("Admin.OrderList.CreatedOn.Header");
			}
			else if (e.Row.DataItem != null)
			{
				var itemData = e.Row.DataItem as Data.Entity.Order;

				var LiteralOrderStatus = e.Row.FindControl("LiteralOrderStatus") as Literal;
				var LiteralPaymentStatus = e.Row.FindControl("LiteralPaymentStatus") as Literal;
				var LiteralShippingStatus = e.Row.FindControl("LiteralShippingStatus") as Literal;
				var LiteralUserName = e.Row.FindControl("LiteralUserName") as Literal;
				var HyperLinkOrder = e.Row.FindControl("HyperLinkOrder") as HyperLink;

				if (_OrdeService == null)
				{
					_OrdeService = new Service.OrderService();
				}
				if (_PaymentMethodService == null)
				{
					_PaymentMethodService = new Service.PaymentMethodService();
				}
				if (_ShippingMethodService == null)
				{
					_ShippingMethodService = new Service.ShippingMethodService();
				}

				LiteralOrderStatus.Text = _OrdeService.GetAllOrderStatus().Where(exp => exp.OrderStatusID == itemData.OrderStatusID).Select(ex => ex.Name).FirstOrDefault();
				LiteralPaymentStatus.Text = _PaymentMethodService.GetAllPaymentStatus().Where(exp => exp.PaymentStatusID == itemData.PaymentStatusID).Select(ex => ex.Name).FirstOrDefault();
				LiteralShippingStatus.Text = _ShippingMethodService.GetAllShippingStatus().Where(exp => exp.ShippingStatusID == itemData.ShippingStatusID).Select(ex => ex.Name).FirstOrDefault();
				LiteralUserName.Text = System.Web.Security.Membership.GetUser(itemData.UserID).UserName;

				HyperLinkOrder.Text = GetLocalResourceString("Admin.OrderList.View.Header");
				HyperLinkOrder.NavigateUrl = "~/Administration/OrderDetail.aspx?orderid=" + itemData.OrderID.ToString();
			}
		}

		protected void ButtonSearch_Click(object sender, EventArgs e)
		{
			if (SiteContext.Current.WorkingLanguage != null)
			{
				if (_OrdeService == null)
				{
					_OrdeService = new Service.OrderService();
				}

				var userIDs = System.Web.Security.Membership.FindUsersByName(TextCustomerName.Text.Trim() + "%").Cast<System.Web.Security.MembershipUser>().Select(exp => exp.ProviderUserKey as Guid?);

				var orderByLang = _OrdeService.GetOrdersByLanguage(SiteContext.Current.WorkingLanguage.LanguageId);

				if (orderByLang != null)
				{
					var orders = (from o in orderByLang
								  where userIDs.Contains(o.UserID)
								  select o);

					if (!string.IsNullOrEmpty(TextStartDate.Text.Trim()))
					{
						var startDate = DateTime.Parse(TextStartDate.Text.Trim());

						if (orders != null)
						{
							orders = orders.Where(ee => ee.CreatedOn > startDate);
						}
					}

					if (!string.IsNullOrEmpty(TextEndDate.Text.Trim()))
					{
						var endDate = DateTime.Parse(TextEndDate.Text.Trim());

						if (orders != null)
						{
							orders = orders.Where(ee => ee.CreatedOn < endDate);
						}
					}

					if (!string.IsNullOrEmpty(TextOrderNumber.Text.Trim()))
					{
						if (orders != null)
						{
							orders = orders.Where(ee => ee.OrderNumber.StartsWith(TextOrderNumber.Text.Trim()));
						}
					}

					if (ListOrderStatus.SelectedIndex > 0)
					{
						if (orders != null)
						{
							orders = orders.Where(ee => ee.OrderStatusID.Value.ToString() == ListOrderStatus.SelectedValue);
						}
					}

					if (ListPaymentStatus.SelectedIndex > 0)
					{
						if (orders != null)
						{
							orders = orders.Where(ee => ee.PaymentStatusID.Value.ToString() == ListPaymentStatus.SelectedValue);
						}
					}

					if (ListShippingStatus.SelectedIndex > 0)
					{
						if (orders != null)
						{
							orders = orders.Where(ee => ee.ShippingStatusID.Value.ToString() == ListShippingStatus.SelectedValue);
						}
					}

					if (orders != null)
					{
						orders = orders.OrderByDescending(exp => exp.CreatedOn);
					}

					GridOrders.DataSource = orders;
					GridOrders.DataBind();
				}
				else
				{
					GridOrders.DataSource = null;
					GridOrders.DataBind();
				}
			}
		}

		private void BindOrderStatus()
		{
			if (_OrdeService == null)
			{
				_OrdeService = new Service.OrderService();
			}

			var orderStatus = _OrdeService.GetAllOrderStatus();

			ListItem item0 = new ListItem("All", "0");
			ListOrderStatus.Items.Add(item0);

			if (orderStatus != null && orderStatus.Count > 0)
			{
				//var lstOrderStatus = orderStatus.ToList();

				//if (lstOrderStatus.Count > 0)
				//{
				foreach (var orderStatusItem in orderStatus)
				{
					ListItem item = new ListItem(GetLocalResourceString(orderStatusItem.Name), orderStatusItem.OrderStatusID.ToString());

					ListOrderStatus.Items.Add(item);
				}
				//}
			}
		}

		private void BindPaymentStatus()
		{
			if (_PaymentMethodService == null)
			{
				_PaymentMethodService = new Service.PaymentMethodService();
			}

			var paymentStatus = _PaymentMethodService.GetAllPaymentStatus();

			ListPaymentStatus.DataSource = paymentStatus;
			ListPaymentStatus.DataBind();

			ListPaymentStatus.Items.Insert(0, new ListItem("All", "0"));
		}

		private void BindShippingStatus()
		{
			ListShippingStatus.Items.Add(new ListItem("All", "0"));

			if (_ShippingMethodService == null)
			{
				_ShippingMethodService = new Service.ShippingMethodService();
			}

			var shippingStatus = _ShippingMethodService.GetAllShippingStatus();

			if (shippingStatus != null && shippingStatus.Count > 0)
			{
				//var lstShippingStatus = shippingStatus.ToList();

				//if (lstShippingStatus.Count > 0)
				//{
					foreach (var shippingStatusItem in shippingStatus)
					{
						ListItem item = new ListItem(GetLocalResourceString(shippingStatusItem.Name), shippingStatusItem.ShippingStatusID.ToString());

						ListShippingStatus.Items.Add(item);
					}
				//}
			}
		}
	}
}